package com.yf.controller.ai.chat;

import com.yf.model.ai.form.AiPromptForm;
import com.yf.rate_limiting.annotation.PreventDuplicateSubmit;
import com.yf.result.Result;
import com.yf.service.chat.IDeepSeekService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * ai 控制器
 *
 * @author : YiFei
 * @since : 2024/6/6 17:34
 */
@Tag(name = "DeepSeek AI 控制器")
@RestController
@RequestMapping("ai/chat/deep_seek")
@RequiredArgsConstructor
public class DeepSeekController {

    private final IDeepSeekService deepSeekService;

    @Operation(summary = "DeepSeek AI 流式请求")
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:chat:stream')")
    @PostMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResponse> stream(@RequestBody @Validated AiPromptForm aiPromptForm) {
        return deepSeekService.callStream(aiPromptForm);
    }

    @Operation(summary = "重试AI输出")
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:chat:retry')")
    @PostMapping(value = "/{conversationId}/retry", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResponse> retry(@PathVariable Long conversationId) {
        return deepSeekService.retry(conversationId);
    }

    @Operation(summary = "删除消息")
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:chat:delete')")
    @DeleteMapping("/{conversationId}")
    public Result<Boolean> deleteMessage(@PathVariable Long conversationId) {
        Boolean result = deepSeekService.deleteMessage(conversationId);
        return Result.judge(result);
    }


}
